package com.xie.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xie.mapper.BaiduWapMapper;
import com.xie.mapper.BaiduWapTjMapper;
import com.xie.mapper.UserMapper;
import com.xie.pojo.BaiduWap;
import com.xie.pojo.BaiduWapTj;
import com.xie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduWapService {
    @Autowired
    BaiduWapMapper baiduWapMapper;

    @Autowired
    BaiduWapTjMapper baiduWapTjMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    public PageInfo<BaiduWap> findAll(Integer pages,Integer pageSize,String key,Integer type){
        Example example=new Example(BaiduWap.class);
        example.createCriteria()
                .orLike("name","%"+key+"%")
                .andEqualTo("status","1")
                .andEqualTo("type",type);
        PageHelper.startPage(pages,pageSize);
        List<BaiduWap> baiduWaps = baiduWapMapper.selectByExample(example);
        PageInfo<BaiduWap> pageInfo=new PageInfo<>(baiduWaps);
        return pageInfo;
    }
    @Cacheable(cacheNames = "phb")
    public List<BaiduWap> findByDesc(){
        List<BaiduWap> baiduWaps=new ArrayList<>();
        if (redisTemplate.hasKey("phb-name") && redisTemplate.hasKey("phb-count")){
            for (int i = 0; i < 10; i++) {
                BaiduWap baiduWap=new BaiduWap();
                String name = redisTemplate.opsForList().index("phb-name", i);
                Integer count = Integer.parseInt(redisTemplate.opsForList().index("phb-count",i));
                baiduWap.setName(name);
                baiduWap.setCount(count);
                baiduWaps.add(baiduWap);
            }
            return baiduWaps;
        }
        return baiduWaps;
    }

    public void updateCount(Integer id) {
        BaiduWap baiduWap = baiduWapMapper.selectByPrimaryKey(id);
        baiduWap.setCount(baiduWap.getCount()+1);
        baiduWapMapper.updateByPrimaryKey(baiduWap);
    }

    @Transactional
    public void insert(BaiduWapTj baiduWapTj) {
        if (baiduWapTj.getZyurl().contains("https://")){
            String zyUrl=baiduWapTj.getZyurl();
            baiduWapTj.setZyurl(zyUrl.replace("https://",""));
        }
        //System.out.println(baiduWapTj.getZyurl());
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.findByUserName(userDetails.getUsername());
        baiduWapTj.setUid(user.getId());
        baiduWapTjMapper.insert(baiduWapTj);
    }

    public List<BaiduWapTj> findTjAll() {
        return baiduWapTjMapper.selectAll();
    }

    @Transactional
    public void sumbitZy(Integer id) {
        BaiduWapTj baiduWapTj = baiduWapTjMapper.selectByPrimaryKey(id);
        BaiduWap baiduWap=new BaiduWap();
        baiduWap.setName(baiduWapTj.getZyname());
        baiduWap.setWww(baiduWapTj.getZyurl());
        baiduWap.setPassword(baiduWapTj.getZypassword());
        baiduWap.setType(baiduWapTj.getType());
        Integer uid = baiduWapTj.getUid();
        User user = userMapper.selectByPrimaryKey(uid);
        baiduWap.setUpload(user.getName());
        baiduWapMapper.insertSelective(baiduWap);
        baiduWapTjMapper.delete(baiduWapTj);
    }

    public void deleteZy(Integer id) {
        BaiduWapTj baiduWapTj = baiduWapTjMapper.selectByPrimaryKey(id);
        baiduWapTjMapper.delete(baiduWapTj);
    }

    public int countAllBaiduWap() {
        return baiduWapMapper.selectCount(null);
    }
    public int countAllBaiduWapTj(){
        return baiduWapTjMapper.selectCount(null);
    }

    public int findByFalse() {
        BaiduWap baiduWap=new BaiduWap();
        baiduWap.setStatus(0);
        return baiduWapMapper.selectCount(baiduWap);
    }

    public List<BaiduWap> findAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return baiduWapMapper.selectAll();
    }

    public void deleteZyWap(Integer id) {
        baiduWapMapper.deleteByPrimaryKey(id);
    }

    public List<BaiduWapTj> findTjAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return baiduWapTjMapper.findAllByTjUser();
    }

    public List<BaiduWap> findByUser(Integer page, Integer limit) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.findByUserName(userDetails.getUsername());
        PageHelper.startPage(page,limit);
        return baiduWapMapper.findByUser(user.getId());
    }

    public int countByUser() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.findByUserName(userDetails.getUsername());
        return baiduWapMapper.countByUser(user.getId());
    }
}
